package com.lanyan.system.controller.api;

import com.lanyan.common.annotation.RepeatSubmit;

import java.math.BigDecimal;

import com.lanyan.common.utils.SecurityUtils;
import com.lanyan.system.service.IBuildingService;
import com.lanyan.system.service.IEstateService;
import io.swagger.annotations.Api;

import java.util.List;
import java.util.Date;

import com.lanyan.common.core.domain.R;
import com.lanyan.common.core.domain.PageR;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import io.swagger.annotations.ApiOperation;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.core.domain.R;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import springfox.documentation.annotations.ApiIgnore;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.domain.AccountAddress;
import com.lanyan.system.domain.vo.AccountAddressVo;
import com.lanyan.system.service.IAccountAddressService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 用户地址Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@Api(tags = {"用户地址"})
@RequestMapping("/api/accountAddress")
public class ApiAccountAddressController extends BaseController {
    @Autowired
    private IAccountAddressService accountAddressService;
    @Autowired
    private IEstateService estateService;
    @Autowired
    private IBuildingService buildingService;

    /**
     * 查询用户地址列表
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "estateId", value = "所在小区", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "buildingId", value = "选择楼栋", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),
    })
    @ApiOperation("查询用户地址列表")
    public R<List<AccountAddress>> list(@ApiIgnore AccountAddress accountAddress) {
        accountAddress.setAccountId(SecurityUtils.getUserId());
        List<AccountAddress> list = accountAddressService.selectAccountAddressList(accountAddress);
        for (AccountAddress item : list) {
            item.setEstate(estateService.getById(item.getEstateId()));
            item.setBuilding(buildingService.getById(item.getBuildingId()));
        }
        return R.ok(list);
    }

    /**
     * 获取用户地址详细信息
     */
    @GetMapping(value = "/info")
    @ApiOperation("获取用户地址详细信息")
    public R<AccountAddress> getInfo(Long accountAddressId) {
        AccountAddress accountAddress = accountAddressService.getById(accountAddressId);
        accountAddress.setEstate(estateService.getById(accountAddress.getEstateId()));
        accountAddress.setBuilding(buildingService.getById(accountAddress.getBuildingId()));
        return R.ok(accountAddress);
    }

    /**
     * 新增用户地址
     */
    @Log(title = "用户地址", businessType = BusinessType.INSERT)
    @PostMapping("save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "phone", value = "联系方式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "estateId", value = "所在小区", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "buildingId", value = "选择楼栋", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "details", value = "详细地址", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "isDefault", value = "是否默认（0不是 1是）", dataType = "String", dataTypeClass = String.class),
    })
    @RepeatSubmit
    @ApiOperation("新增用户地址")
    public R<AccountAddress> add(@ApiIgnore @RequestBody AccountAddress accountAddress) {
        accountAddress.setAccountId(SecurityUtils.getUserId());
        StringUtils.insertRequired(accountAddress);
        if (StringUtils.equals(accountAddress.getIsDefault(),"1")){
            initDefault();
        }
        accountAddressService.insertAccountAddress(accountAddress);
        return R.ok(accountAddress);
    }

    /**
     * 修改用户地址
     */
    @Log(title = "用户地址", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountAddressId", value = "所属地址", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "phone", value = "联系方式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "estateId", value = "所在小区", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "buildingId", value = "选择楼栋", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "details", value = "详细地址", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "isDefault", value = "是否默认（0不是 1是）", dataType = "String", dataTypeClass = String.class),
    })
    @RepeatSubmit
    @ApiOperation("修改用户地址")
    public AjaxResult edit(@ApiIgnore @RequestBody AccountAddress accountAddress) {
//        StringUtils.updateRequired(accountAddress);
        if (StringUtils.equals(accountAddress.getIsDefault(),"1")){
            initDefault();
        }
        accountAddressService.updateAccountAddress(accountAddress);
        return success("修改成功");
    }

    /**
     * 删除用户地址
     */
    @Log(title = "用户地址", businessType = BusinessType.DELETE)
    @GetMapping("/del")
    @ApiOperation("删除用户地址")
    public AjaxResult remove(Long accountAddressId) {
        accountAddressService.deleteAccountAddressByAccountAddressId(accountAddressId);
        return success("删除成功");
    }

    public void initDefault(){
        accountAddressService.lambdaUpdate()
                .eq(AccountAddressVo::getDelFlag,"0")
                .eq(AccountAddressVo::getAccountId,SecurityUtils.getUserId())
                .set(AccountAddressVo::getIsDefault,"0")
                .update();
    }
}
